<?php

class ConfigManager implements ArrayAccess
{

  private static $instance;
  
  private $config;

  /*
   * region: ISingleton implement
   */

  private function __construct($filename = 'Web.config')
  {
    $path = my()->directory;
    $this->config = require_once ("{$path}{$filename}.php");
  }

  private function __clone()
  {

  }

  public static function ConfigManager()
  {
    return (!isset(self::$instance)) ? (self::$instance = new self()) : self::$instance;
  }

  public function __get($name)
  {
    if (key_exists($name, $this->config)) {
      return $this->config[$name];
    }

    throw new Exception("{$name} config section not found");
  }

  /*
   * ArrayAccess implementaion
   */

  public function offsetSet($offset, $value)
  {
    if (is_null($offset)) {
      $this->config[] = $value;
    } else {
      $this->config[$offset] = $value;
    }
  }

  public function offsetExists($offset)
  {
    return isset($this->config[$offset]);
  }

  public function offsetUnset($offset)
  {
    unset($this->config[$offset]);
  }

  public function offsetGet($offset)
  {
    return isset($this->config[$offset]) ? $this->config[$offset] : null;
  }

}